/**
 * 定义天数据模块
 */
(function(window, angular, undefined){
	var module = angular.module('water.apps.voc.ReportData.displaceStatistics.monthDataManager', ['water.entity.WasteGas.wasteGasMonitorData',
	                     	                                                                          'water.common.dateUtil']);
	
	module.controller('displaceStatisticsMonthController',function($scope,$wasteGasMonitorDataService,$controller,$dateUtil){
		
		/**
		 * 开始时间
		 */
		$scope.startDate;
		/**
		 * 界面上冻结表格的组件
		 */
		$scope.freezeGrid;
		
		/**
		 * 监听左侧列表选择
		 */
		$scope.$on('dataPramChanged', function(event, station) {
			$scope.onStationSelectedBeforeQuery(station);
			var queryObject = $scope.getQueryObject();
			$scope.getReport(queryObject);
	    });
		
		$scope.onSearchClick = function(){
			var queryObject = $scope.getQueryObject();
			$scope.getReport(queryObject);
		}
		
		$scope.onExportExcelClick = function(){
			var queryObject = $scope.getQueryObject();
			$wasteGasMonitorDataService.exportCouMonthReportAIRWord(queryObject);
		}
		
		
		/**
		 * 站点选择后，报表查询前执行的逻辑
		 * 用于准备查询前的参数
		 */
		$scope.onStationSelectedBeforeQuery = function(station){
			$scope.selectedModel = station;
		}
		
		/**
		 * 查询报表
		 */
		$scope.getReport = function(queryObject){
			$wasteGasMonitorDataService.getCouMonthReportAIR(queryObject).then(function(data){
				$scope.data = data;
				//解决无数据时，样式不对的BUG
				if($scope.data.data.length==0)
				{  
					$scope.data.data.push(1);
				}
				console.log(data);
			});
		}
		
		/**
		 * 获取查询参数
		 * forExcel 是否用于导出
		 */
		$scope.getQueryObject = function(){
			var parmsStr = $scope.selectedModel.parmToChecksStr;
			var queryObject = {
					"MN":$scope.selectedModel.MN,
				};
			if($scope.dataType == "month"){
				var date = new Date();
				date.setFullYear($scope.selectYear);
				date.setMonth(parseInt($scope.selectMonth) - 1);
				date.setDate(1);
				date.setMonth(parseInt($scope.selectMonth));
				queryObject.START =$scope.selectYear + "-" + $scope.selectMonth + "-01" + " 00:00:00";
				//获取每个月最后一天
				var end_day = new Date($scope.selectYear,$scope.selectMonth,0); 
				queryObject.END =$scope.selectYear + "-" + $scope.selectMonth + "-"+end_day.getDate() + " 23:59:59";
			}
			if($scope.serachType == "all")
			{
				queryObject.MN = "all";
			}
			
			return queryObject;
		}
		
		
		$scope.getDisplaceRadio = function() {
			var minRadio=$("#mt").prop("checked");
			var hourRadio=$("#hr").prop("checked");
			var dayRadio=$("#dy").prop("checked");
			var monthRadio=$("#month").prop("checked");
			
			if(minRadio||hourRadio||dayRadio||monthRadio){
				$("#displaceStatistics").prop("checked","true");
			}	
		};
		$scope.getDisplaceRadio();
		
		$scope.getSerachType = function(serachType) {
			$scope.serachType = serachType;
		};
		
		
		//初始化日期
		$scope.initQuery = function(dataType){
			$scope.dataType = dataType;
			if($scope.dataType == "day"){
				$scope.numLimit=13;
			}
			else if($scope.dataType == "month"){
				$scope.numLimit=7;
			}
			// 周查询条件，其中年份是公用的
			$scope.yearlist=[];
			var currentYear=(new Date()).getFullYear();
			for(var i=0;i<4;i++){
				$scope.yearlist.push({
					name:currentYear-i+"年",
					value:currentYear-i+""
				});
			}; 
			$scope.selectYear=currentYear+"";
			// 月查询条件
			$scope.monthlist=[];
			for(var i=1;i<=12;i++){
				$scope.monthlist.push({
					name:i+"月",
					value:""+i
				});
			};
			$scope.selectMonth=((new Date()).getMonth() + 1)+"";
			$scope.getReport();
		};
		
		function _init(){
			$scope.initQuery("month");
			$("#all").click();
			$scope.serachType = "all";
		}
		
		_init();
		
	});
}(window, angular));	